<template>
  <el-dialog
    append-to-body
    :title="info+'-会员'"
    :visible.sync="dialogVisible"
    align="left"
    v-loading="currLoading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    width="600px">
    <el-form ref="form" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="用户名" align="left" prop="username">
        <el-input v-model="form.username" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="昵称" align="left" prop="nickname">
        <el-input v-model="form.nickname" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="头像" align="left" prop="icon">
        <el-upload
          class="avatar-uploader"
          :action="this.UploadFileUrl"
          name="myUploadFile"
          :show-file-list="false"
          :on-success="uploadIconSuccess">
          <img v-if="form.icon" :src="form.icon" class="avatar" style="width: 50px">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-input v-model="form.icon" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="密码" align="left" prop="password">
        <el-input type="password" v-model="form.password" show-password style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="性别" align="left" prop="sex">
        <el-select v-model="form.sex" placeholder="性别">
          <el-option
            v-for="(value,index) in sexArr"
            :key="index"
            :label="value"
            :value="value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>

export default {
  name: 'CreateMemberDialog',
  components: {},
  props:{
    successFunction:Function
  },
  data: function () {
    return {
      info:'创建',
      dialogVisible: false,
      currLoading: false,
      sexArr:[],
      form: {
        id:0,
        username: '',
        nickname: '',
        password: '',
        icon: '',
        sex: '男',
      },
      rules: {
        username: [
          {required: true, message: '请输入 用户名', trigger: ['blur', 'change']},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: ['blur', 'change']}
        ],
        nickname: [
          {required: true, message: '请输入 昵称', trigger: ['blur', 'change']},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: ['blur', 'change']}
        ],
        password: [
          {required: true, message: '请输入 密码', trigger: ['blur', 'change']},
          {min: 6, max: 30, message: '长度在 6 到 30 个字符', trigger: ['blur', 'change']}
        ]
      }
    }
  },
  mounted () {
    var usernameRule = this.getFieldTypeArr('username', DemoData.Member.field, 'rule')
    var passwordRule = this.getFieldTypeArr('password', DemoData.Member.field, 'rule')

    this.rules.username = [
      {required: true, message: '请输入 用户名', trigger: ['blur', 'change']},
      {
        type: 'string',
        pattern: new RegExp(usernameRule.pattern),
        message: '长度在 ' + usernameRule.min + ' 到 ' + usernameRule.max + ' 个字符',
        trigger: ['blur', 'change']
      }
    ]
    this.rules.password = [
      {
        min: passwordRule.min,
        max: passwordRule.max,
        message: '长度在 ' + passwordRule.min + ' 到 ' + passwordRule.max + ' 个字符',
        trigger: ['blur', 'change']
      }
    ]
  },
  methods: {
    uploadIconSuccess:function (res) {
      console.log('上传完毕',res)
      this.form.icon=res.data
    },
    open: function (userInfo) {
      this.info='创建'
      if (userInfo.id) {
        this.info='更新'
        this.form.id = userInfo.id
        this.form.username = userInfo.username
        this.form.nickname = userInfo.nickname
        this.form.icon = userInfo.icon
        this.form.sex = userInfo.sex
        this.rules.password = [
          {min: 6, max: 30, message: '长度在 6 到 30 个字符', trigger: ['blur', 'change']}
        ]
      }else{
        this.form={
          id:0,
          username: '',
          nickname: '',
          password: '',
          icon: '',
          sex: '男',
        }
        if (this.$refs.form){
          this.$refs.form.resetFields()
        }
      }
      this.dialogVisible = true
      this.$forceUpdate()
    },
    submit: function () {
      let $this = this
      $this.$refs.form.validate((valid) => {
        if (valid) {
          var api = 'Member/create'
          var form = Object.assign({}, $this.form)
          if (this.form.id > 0) {
            api = 'Member/update'
            if (!$this.form.password) {
              delete form.password
            }
          }
          $this.currLoading = true
          $this.MyRequest(this.ApiDoNameSystem + api, $this.form, function () {
            $this.$message.success($this.info+'成功')
            $this.currLoading = false
            $this.form = {
              id:0,
              username: '',
              nickname: '',
              password: '',
            }
            $this.dialogVisible=false
            $this.$props.successFunction()
            $this.$forceUpdate()
          }, function () {
            $this.$message.success('操作失败，请重试')
            $this.currLoading = false
            $this.$forceUpdate()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
